$(document).ready(function () {
    //  alert("aaa")
    selectWarning()
    selectWarningAll()
});

//动态查询
function selectWarning() {
    $(document).on('click', '#selectWarning', function () {
        var termYear = $("#termYear option:selected").val();
        var termNum = $("#termNum option:selected").val();
        var url = "http://localhost:8080/warning/selectWarning?termYear=" + termYear + "&termNum=" + termNum;
        if (termYear == "" && termNum == "") {
            layer.alert("请选择学年和学期");
            return false;
        }
        if (termYear == "" && termNum != "") {
            layer.alert("请选择学年");
            return false;
        }
        if (termNum == "" && termYear != "") {
            layer.alert("请选择学期");
            return false;
        }
        var table = $('#dataTable').dataTable()
        if (table) {
            table.fnDestroy();
        }

        $('#dataTable').dataTable({
            "processing": true,
            "serverSide": true,
            "bInfo": false,//是否显示是否启用底边信息栏
            "sAjaxDataProp": "data.markConditionList",
            "sAjaxSource": url,
            "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
            "fnPreDrawCallback": function (oSettings) {
                $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
                $('.dataTables_length select').addClass('form-control input-small');
            },
            "oLanguage": {
                "sSearch": "",
                "sLengthMenu": "<span>_MENU_ entries</span>",
                "sZeroRecords": "暂无相关信息",
            },
            "bJQueryUI": false,
            'bAutoWidth': false,
            "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",
            "aoColumnDefs": [
                {
                    "mDataProp": null,
                    "sDefaultContent": "<a href='#'><span  class='label label-danger mr10 mb10'  onclick='addWarning()'>添加学业警示</span></a>",
                    "aTargets": [-1]
                }
            ],
            "aoColumns": [
                {"mDataProp": "stuNum"},
                {"mDataProp": "stuName"},
                {"mDataProp": "termYear"},
                {"mDataProp": "termNum"},
                {"mDataProp": "actualCredithour"},
                {"mDataProp": "stuCourseCresithour"},
                {
                    "mDataProp": "warningNum",
                    "mRender": function (data) {
                        //data=1录完，data=0没录
                        if (data == 0) {
                            return "<a href='#'><span  class='label label-danger mr10 mb10' id='addwarning' >添加学业警示</span></a>";
                        }
                        if (data == 1) {
                            return "<span  class='badge badge-info mr10 mb10'>已添加学业警示</span>";
                        }

                    }
                }
            ],
        })

    });
}

//显示全部已经得了学业警示的人
function selectWarningAll() {
    var url = "http://localhost:8080/warning/studentWarning";
    var table = $('#dataTable').dataTable()
    if (table) {
        table.fnDestroy();
    }

    $('#dataTable').dataTable({
        "processing": true,
        "serverSide": true,
        "bInfo": false,//是否显示是否启用底边信息栏
        "sAjaxDataProp": "data.warningList",
        "sAjaxSource": url,
        "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
        "fnPreDrawCallback": function (oSettings) {
            $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
            $('.dataTables_length select').addClass('form-control input-small');
        },
        "oLanguage": {
            "sSearch": "",
            "sLengthMenu": "<span>_MENU_ entries</span>",
            "sZeroRecords": "暂无相关信息",
        },
        "bJQueryUI": false,
        'bAutoWidth': false,
        "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",
        "aoColumnDefs": [
            {
                "mDataProp": null,
                "sDefaultContent": "<a href='#'><span  class='label label-danger mr10 mb10'  onclick='addWarning()'>添加学业警示</span></a>",
                "aTargets": [-1]
            }
        ],
        "aoColumns": [
            {"mDataProp": "stuNum"},
            {"mDataProp": "stuName"},
            {"mDataProp": "termYear"},
            {"mDataProp": "termNum"},
            {"mDataProp": "actualCredithour"},
            {"mDataProp": "stuCourseCresithour"},
            {
                "mDataProp": "warningNum",
                "mRender": function (data) {
                    //data=1录完，data=0没录
                    if (data == 0) {
                        return "<a href='#'><span  class='label label-danger mr10 mb10' id='addwarning' >添加学业警示</span></a>";
                    }
                    if (data == 1) {
                        return "<span  class='badge badge-info mr10 mb10'>已添加学业警示</span>";
                    }

                }
            }
        ],
    })
}

//添加学业警示，warningNum=1已添加学业警示，warningNum=0未添加学业警示
$(document).on('click', '#addwarning', function () {
    var stuNum = $(this).parents("tr").children(0).eq(0).text();
    var termYear = $(this).parents("tr").children(0).eq(2).text();
    var termNum = $(this).parents("tr").children(0).eq(3).text();
  //  alert("-stuNum-" + stuNum + "-termYear-" + termYear + "-termNum-" + termNum)
    $.ajax({
        url: "http://localhost:8080/warning/addWarning?termYear=" + termYear + "&termNum=" + termNum + "&stuNum=" + stuNum + "&warningNum=" + 1,
        type: "GET",
        async: false,
        dataType: "json",
        success: function (data) {
            layer.alert("添加成功!");
            window.location.reload();
        },
        error: function () {
            layer.alert("你的网络太慢了！添加失败!再试一次吧！");
        }
    });
})

//刷新页面
function reloadPage() {
    window.location.reload();
}

